package com.example.asus.recyclerview;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements  IOnDelListener{
    private RecyclerView rlvStu;
    private ArrayList<Student> studentArrayList=new ArrayList<>();
    private ArrayList<Student> searchResultList=new ArrayList<>();
    private StuAdapter adapter;
    private Context context;
    private Button btnAddStu;
    private EditText edtKeyword;
    private Button btnSearch;
    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rlvStu=findViewById(R.id.rlv_stu);

        initStudentArrayList();

        initAdapter();

        initAddButton();

        initSearchView();

    }

    /*  初始化搜索 */
    private void initSearchView() {
        edtKeyword =findViewById(R.id.edt_keyword);
        btnSearch = findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyword = edtKeyword.getText().toString().trim();
                // 使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
            /*使用学生列表和关键字 得到搜索结果列表*/
            private void search(final ArrayList<Student> studentArrayList,
                                String keyword) {
                searchResultList.clear();
                for(int i=0; i<studentArrayList.size(); i++){
                    Student s = studentArrayList.get(i);
                    // 如果学生姓名包含了关键字 这个学生就加入到结果列表
                    if(s.getName().contains(keyword)){
                        searchResultList.add(s);
                    }
                    // 如果学生年龄等于关键字 这个学生就加入到结果列表
                    else if(String.valueOf(s.getAge()).equals(keyword)){
                        searchResultList.add(s);
                    }
                    edtKeyword.addTextChangedListener(new TextWatcher() {

                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }
                        public void afterTextChanged(Editable editable) {
                            String keyword = edtKeyword.getText().toString().trim();
                            // 使用学生列表和关键字 得到搜索结果列表
                            search(studentArrayList, keyword);
                            adapter.changeData(searchResultList);
                            isResultList = true;
                        }
                    });
                }
            }
        });
    }

    /*  初始化适配器 */
    private void initAdapter() {
        adapter = new StuAdapter(MainActivity.this,
                studentArrayList,  MainActivity.this);
        rlvStu.setAdapter(adapter);


        LinearLayoutManager manager=new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        Log.e("MainActivity",
                "requestCode="+requestCode+"resultCode="+resultCode);
        if(data==null){
            return;
        }
        if(resultCode == 2001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.yi);

            Student s = new Student("18006",name,age,imgId);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }

        else if(resultCode == 3001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.yi);
            String no = data.getStringExtra("NO");

            for(int i=0; i<studentArrayList.size(); i++){
                Student s = studentArrayList.get(i);

                if(s.getNo().equals(no)) {
                    s.setName(name);
                    s.setAge(age);
                    s.setImgId(imgId);
                }
            }
        }
    }

    /*  初始化添加学生按钮 */
    private void initAddButton() {
        btnAddStu=findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
    }

    /*  初始化学生列表 */
    private void initStudentArrayList() {
        Student s1=new Student("01","小千玺",18,R.drawable.yi);
        Student s2=new Student("02","宋闵浩",19,R.drawable.song);
        Student s3=new Student("03","功必扬",17,R.drawable.gong);
        Student s4=new Student("04","李山花",20,R.drawable.gray);
        Student s5=new Student("05","南柱赫",22,R.drawable.nan);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }
    public void del(int i){
        Log.e("MainActivity","del"+i);
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
